package rentadomus

class SearchByFiltersController {

    def index() { 
        render(action: "main")
    }
    def main(){
        def type = ["------------","Apartment", "House", "Local","Office", "WareHouse"]
        def type2 = ["----------","Area", "Price", "Neighborhood", "Strata"]
        def rec2 = params.filter
        def fromArea;
        def toArea;
        if(rec2!=null && rec2=="Area"){
            fromArea=["----","20","40", "60", "80", "100","200"]
            toArea=["----","60", "80", "100", "120", "140", "180", "200", "300",
            "400","500","600","700"]
        }
        else{
            fromArea=["-------","50000","100000", "200000", "300000", "400000",
            "500000","600000","700000","800000","900000","1000000","2000000","3000000"
            ,"4000000","5000000"]
            toArea=["--------","50000","100000", "200000", "300000", "400000",
            "500000","600000","700000","800000","900000","1000000","2000000","3000000"
            ,"4000000","5000000","10000000","15000000"]
        }
        def rec = params.CBPurpose
        def sVal = params.sValue;
        def sVariable=0
        def fArea = params.fArea
        def tArea = params.tArea
        
            // this redirects to the correspondent form depending of the type of place
        switch(rec)
        {

            case "House":
                switch(rec2){
                    case "Area":
                        sVariable="1";
                        sVal=null
                        if(!(fArea ==null || tArea == null)){
                            if( !(tArea == "----" ||
                                    fArea == "----" || fArea =="-------" || tArea == "--------" ||
                                    Integer.parseInt(fArea)>Integer.parseInt(tArea))){
                                redirect(controller:"House", action: "filterSearch", 
                                    params:[value:fArea,value2:tArea, filter:rec2, option:sVariable])
                            }
                        }
                        else{
                        }
                        break;
                    case "Price":
                        sVariable="1";
                        sVal=null
                        if(!(fArea ==null || tArea == null)){
                            if( !(tArea == "----" ||
                                    fArea == "----" || fArea =="-------" || tArea == "--------" ||
                                    Integer.parseInt(fArea)>Integer.parseInt(tArea))){
                                redirect(controller:"House", action: "filterSearch", 
                                    params:[value:fArea,value2:tArea, filter:rec2, option:sVariable])
                            }
                        }
                        else{
                        }
                        break;
                    case "Neighborhood":
                        sVariable="3"
                        if(!(sVal==null || sVal.equals("") || sVariable==0)){
                            redirect(controller:"House", action: "filterSearch", 
                                params:[value:sVal, filter:rec2,option:sVariable])
                        }
                        break;
                    case "Strata":
                        sVariable="3"
                        try{
                        def i = Integer.parseInt(sVal)
                        if(!(sVal==null || sVal.equals("") || sVariable==0)){
                            redirect(controller:"House", action: "filterSearch", 
                                params:[value:sVal, filter:rec2,option:sVariable])
                        }
                        }
                        catch(Exception){
                            
                        }
                        
                        break;

                }
                 
                break;
            case "Apartment":
                switch(rec2){
                    case "Area":
                        sVariable="1";
                        sVal=null
                        if(!(fArea ==null || tArea == null)){
                            if( !(tArea == "----" ||
                                    fArea == "----" || fArea =="-------" || tArea == "--------" ||
                                    Integer.parseInt(fArea)>Integer.parseInt(tArea))){
                                redirect(controller:"Apartament", action: "filterSearch", 
                                    params:[value:fArea,value2:tArea, filter:rec2, option:sVariable])
                            }
                        }
                        break;
                    case "Price":
                        sVariable="1"
                        sVal=null
                        if(!(fArea ==null || tArea == null)){
                            if( !(tArea == "----" ||
                                    fArea == "----" || fArea =="-------" || tArea == "--------" ||
                                    Integer.parseInt(fArea)>Integer.parseInt(tArea))){
                                redirect(controller:"Apartament", action: "filterSearch", 
                                    params:[value:fArea,value2:tArea, filter:rec2, option:sVariable])
                            }
                        }
                        break;
                    case "Neighborhood":
                        sVariable="3"
                        if(!(sVal==null || sVal.equals("") || sVariable==0)){
                            redirect(controller:"Apartament", action: "filterSearch", 
                                params:[value:sVal, filter:rec2,option:sVariable])
                          }       
                        break;
                    case "Strata":
                        sVariable="3"
                        try{
                        def i = Integer.parseInt(sVal)
                        if(!(sVal==null || sVal.equals("") || sVariable==0)){
                            redirect(controller:"Apartament", action: "filterSearch", 
                                params:[value:sVal, filter:rec2,option:sVariable])
                        }
                        }
                        catch(Exception){
                            
                        }
                        
                        break;
                        

                }
                break;

            case "Local":
                switch(rec2){
                    case "Area":
                        sVariable="1";
                        sVal=null
                        if(!(fArea ==null || tArea == null)){
                            if( !(tArea == "----" ||
                                    fArea == "----" || fArea =="-------" || tArea == "--------" ||
                                    Integer.parseInt(fArea)>Integer.parseInt(tArea))){
                                redirect(controller:"Local", action: "filterSearch", 
                                    params:[value:fArea,value2:tArea, filter:rec2, option:sVariable])
                            }
                        }
                        else{
                        }
                        
                        break;
                    case "Price":
                        sVariable="1"
                        sVal=null
                        if(!(fArea ==null || tArea == null)){
                            if( !(tArea == "----" ||
                                    fArea == "----" || fArea =="-------" || tArea == "--------" ||
                                    Integer.parseInt(fArea)>Integer.parseInt(tArea))){
                                redirect(controller:"Local", action: "filterSearch", 
                                    params:[value:fArea,value2:tArea, filter:rec2, option:sVariable])
                            }
                        }
                        break;
                    case "Neighborhood":
                        sVariable="3"
                        if(!(sVal==null || sVal.equals("") || sVariable==0)){
                            redirect(controller:"Local", action: "filterSearch", 
                                params:[value:sVal, filter:rec2,option:sVariable])
                         }
                        break;
                    case "Strata":
                        sVariable="3"
                        try{
                        def i = Integer.parseInt(sVal)
                        if(!(sVal==null || sVal.equals("") || sVariable==0)){
                            redirect(controller:"Local", action: "filterSearch", 
                                params:[value:sVal, filter:rec2,option:sVariable])
                        }
                        }
                        catch(Exception){
                            
                        }
                        break;

                }
                
                break;

            case "Office":
                switch(rec2){
                    case "Area":
                        sVariable="1";
                        sVal=null
                        if(!(fArea ==null || tArea == null)){
                            if( !(tArea == "----" ||
                                    fArea == "----" || fArea =="-------" || tArea == "--------" ||
                                    Integer.parseInt(fArea)>Integer.parseInt(tArea))){
                                redirect(controller:"Office", action: "filterSearch", 
                                    params:[value:fArea,value2:tArea, filter:rec2, option:sVariable])
                            }
                        }
                        else{
                        }
                        break;
                    case "Price":
                        sVariable="1"
                        sVal=null
                        if(!(fArea ==null || tArea == null)){
                            if( !(tArea == "----" ||
                                    fArea == "----" || fArea =="-------" || tArea == "--------" ||
                                    Integer.parseInt(fArea)>Integer.parseInt(tArea))){
                                redirect(controller:"Office", action: "filterSearch", 
                                    params:[value:fArea,value2:tArea, filter:rec2, option:sVariable])
                            }
                        }
                        break;
                    case "Neighborhood":
                        sVariable="3"
                        if(!(sVal==null || sVal.equals("") || sVariable==0)){
                            redirect(controller:"Office", action: "filterSearch", 
                                params:[value:sVal, filter:rec2,option:sVariable])
                         }
                        break;
                    case "Strata":
                        sVariable="3"
                        try{
                        def i = Integer.parseInt(sVal)
                        if(!(sVal==null || sVal.equals("") || sVariable==0)){
                            redirect(controller:"Office", action: "filterSearch", 
                                params:[value:sVal, filter:rec2,option:sVariable])
                        }
                        }
                        catch(Exception){
                            
                        }
                        break;

                }
                
                break;

            case "WareHouse":
                
                switch(rec2){
                    case "Area":
                        sVariable="1";
                        sVal=null
                        if(!(fArea ==null || tArea == null)){
                            if( !(tArea == "----" ||
                                    fArea == "----" || fArea =="-------" || tArea == "--------" ||
                                    Integer.parseInt(fArea)>Integer.parseInt(tArea))){
                                redirect(controller:"Warehouse", action: "filterSearch", 
                                    params:[value:fArea,value2:tArea, filter:rec2, option:sVariable])
                            }
                        }
                        break;
                    case "Price":
                        sVariable="1";
                        sVal=null
                        if(!(fArea ==null || tArea == null)){
                            if( !(tArea == "----" ||
                                    fArea == "----" || fArea =="-------" || tArea == "--------" ||
                                    Integer.parseInt(fArea)>Integer.parseInt(tArea))){
                                redirect(controller:"Warehouse", action: "filterSearch", 
                                    params:[value:fArea,value2:tArea, filter:rec2, option:sVariable])
                            }
                        }
                        break;
                    case "Neighborhood":
                        sVariable="3"
                        if(!(sVal==null || sVal.equals("") || sVariable==0)){
                            redirect(controller:"Warehouse", action: "filterSearch",
                                params:[value:sVal, filter:rec2,option:sVariable])
                        }
                        break;
                    case "Strata":
                        sVariable="3"
                        try{
                        def i = Integer.parseInt(sVal)
                        if(!(sVal==null || sVal.equals("") || sVariable==0)){
                            redirect(controller:"Warehouse", action: "filterSearch", 
                                params:[value:sVal, filter:rec2,option:sVariable])
                        }
                        }
                        catch(Exception){
                            
                        }
                        break;

                }
                
                break;
            
        }
        
        
        [sFilter:type2,sType:type, sValue:sVal, fArea:fromArea, tArea:toArea, variables:sVariable,
        recValue:rec,recValue2:rec2]

    }
    
}
